package 集合框架;

import java.util.*;

//Collection->List->Vector
public class Demo1 {

    //1.产生集合
    public static void a(){
        //产生一个集合(空)
        Vector<Object> v1 = new Vector<>();
        //产生一个集合(空),只能存int型的数据。
        Vector<Integer> v2 = new Vector<>();
        //产生一个集合(空),只能村String型的数据。
        Vector<String> v3 = new Vector<>();

        System.out.println(v1);
        System.out.println(v2);
        System.out.println(v3);
    }

    //2.对集合进行CRUD(存放、获取、修改、删除)
    public static void b(){
        Vector<String> vector = new Vector<>();
        //1.存放
        vector.add("aa");//aa
        vector.add("bb");//aa bb
        vector.add("cc");//aa bb cc
        vector.add("dd");//aa bb cc dd
        vector.add("ee");//aa bb cc dd ee
        vector.add(null);//aa bb cc dd ee null
        System.out.println(vector);
        vector.add(2,"zz");
        System.out.println(vector);//aa, bb, zz, cc, dd, ee, null
        //2.获取
        String s4 = vector.get(2);//zz
        System.out.println(s4);
        //3.修改
        vector.set(2,"tt");
        System.out.println(vector);
        //4.删除
        vector.remove(2);
        vector.remove("ss");
        System.out.println(vector);
    }
    //3.其他方法
    public static void c(){
        Vector<String> vector = new Vector<>();
        vector.add("aa");//aa
        vector.add("bb");//aa bb
        vector.add("cc");//aa bb cc
        vector.add("dd");//aa bb cc dd
        vector.add("ee");//aa bb cc dd ee
        vector.add("ff");//aa bb cc dd ee ff
        //获取长度
        int size = vector.size();
        //是否为空 size == 0 true false
        boolean empty = vector.isEmpty();
        //清空
        //vector.clear();
        //从指定位置截取产生新的集合(包头不包尾)
        List<String> subList = vector.subList(1, 3);
        String first = vector.firstElement();
        String last = vector.lastElement();
        String s2 = vector.elementAt(2);
        //将集合转换为数组
        Object[] array = vector.toArray();
        System.out.println(size);
        System.out.println(empty);
        System.out.println(vector);
        System.out.println(subList);
        System.out.println(first);
        System.out.println(last);
        System.out.println(s2);
        System.out.println(Arrays.toString(array));
    }

    //4.循环集合
    public static void d1(){
        Vector<String> vector = new Vector<>();
        vector.add("aa");//aa
        vector.add("bb");//aa bb
        vector.add("cc");//aa bb cc
        vector.add("dd");//aa bb cc dd
        vector.add("ee");//aa bb cc dd ee
        vector.add("ff");//aa bb cc dd ee ff
        for (int i = 0; i < vector.size(); i++) {
            String s = vector.get(i);
            System.out.print(s+"\t");
        }
    }
    public static void d2(){
        Vector<String> vector = new Vector<>();
        vector.add("aa");//aa
        vector.add("bb");//aa bb
        vector.add("cc");//aa bb cc
        vector.add("dd");//aa bb cc dd
        vector.add("ee");//aa bb cc dd ee
        vector.add("ff");//aa bb cc dd ee ff
        //forEach(增强for)简写
        for (String s:vector){
            System.out.println(s+"\t");
        }
    }
    public static void d3(){
        Vector<String> vector = new Vector<>();
        vector.add("aa");//aa
        vector.add("bb");//aa bb
        vector.add("cc");//aa bb cc
        vector.add("dd");//aa bb cc dd
        vector.add("ee");//aa bb cc dd ee
        vector.add("ff");//aa bb cc dd ee ff
        //forEach(增强for)标准
        Iterator<String> iterator = vector.iterator();
        while(iterator.hasNext()){
            String s = iterator.next();
            System.out.println(s+"\t");
        }
    }
    public static void d4(){
        Vector<String> vector = new Vector<>();
        vector.add("aa");//aa
        vector.add("bb");//aa bb
        vector.add("cc");//aa bb cc
        vector.add("dd");//aa bb cc dd
        vector.add("ee");//aa bb cc dd ee
        vector.add("ff");//aa bb cc dd ee ff
        //forEach(增强for)lambda
        vector.forEach(s->{
            System.out.print(s+"\t");
        });
    }
    public static void d5(){
        Vector<String> vector = new Vector<>();
        vector.add("aa");//aa
        vector.add("bb");//aa bb
        vector.add("cc");//aa bb cc
        vector.add("dd");//aa bb cc dd
        vector.add("ee");//aa bb cc dd ee
        vector.add("ff");//aa bb cc dd ee ff
        vector.forEach(System.out::println);
    }
    public static Scanner scanner = new Scanner(System.in);

    //5.排序、查找
    public static void e1(){
        Vector<Integer> vector = new Vector<>();
        for (int i = 0; i < 15; i++) {
            vector.add((int)(Math.random()*10+1));
        }
        System.out.println("集合的内容是:"+vector);
        System.out.println("请输入一个数:");
        int number = scanner.nextInt();
        //判断number在集合中是否存在
        boolean contains = vector.contains(number);
        System.out.println(contains?"存在":"不存在");
        //找出number在集合中第一次出现的下标,不存在返回-1
        int index1 = vector.indexOf(number);
        System.out.println(index1);
        //找出number在集合中最后一次出现的下标,不存在返回-1
        int index2 = vector.lastIndexOf(number);
        System.out.println(index2);
        //找出number在集合第五个位置开始第一次出现的下标,不存在返回-1
        int index3 = vector.indexOf(number,4);
        System.out.println(index3);
    }
    //练习:z找出集合中5出现的所有位置
    public static void work1(){
        Vector<Integer> vector = new Vector<>();
        for (int i = 0; i < 20; i++) {
            vector.add((int)(Math.random()*10+1));
        }
        System.out.println(vector);
        int num = 5;
        int begin = 0;
        while(true){
            int index = vector.indexOf(num,begin);
            if (begin == vector.size()){
                break;
            }
            if (index == -1){
                break;
            }
            begin = index + 1;
            System.out.println("元素位置是:"+index);
        }
    }

    public static void work2(){
        Vector<Integer> vector = new Vector<>();
        for (int i = 0; i < 20; i++) {
            vector.add((int)(Math.random()*10+1));
        }
        System.out.println(vector);
        work3(vector, 0, 5);
    }

    public static void work3(Vector<Integer> vector, int begin ,int num){
        int index = vector.indexOf(num, begin);
        if (begin>=vector.size()){
            return;
        }
        if (index==-1){
            return;
        }
        begin = index + 1;
        System.out.println("元素位置是:"+index);
        work3(vector, begin, num);
    }

    public static void main(String[] args) {
//        a();
//        b();
//        c();
//        e1();
        work2();
    }
}
